package algebra.visitors.cursors;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import option.KBVocabulary;

import algebra.op.PrimaryOP;

import kb.DescriptionLogicsKB;
import entities.Individual;
import entities.assertion.ConceptAssertion;

public class CACursorPI extends CACursor {
	private List<Individual> allinds;
	private final DescriptionLogicsKB kb;
	private int curPos = 0;
	private final static boolean sortInds = false;

	public CACursorPI(PrimaryOP op) {
		this.kb = op.getPlanEvalKB();
	}

	@Override
	public boolean openCursor() {
		this.curPos = 0;
		if (kb == null) {
			System.out.println("No KB found to evaluate primary index.");
			return false;
		}
		allinds = kb.getAbox().getListOfAllIndividuals();
//		System.err.println("total: " + allinds.size());
		if (allinds.size() < 1) {
			return false;
		}
		if(sortInds){
			Collections.sort(allinds, new Comparator<Individual> () {
				public int compare(Individual one, Individual another) {
					return one.getName().compareTo(another.getName());
				}
			});
		}
		return true;
	}

	@Override
	public ConceptAssertion getNextResult() {
		if (allinds!= null && this.curPos >= 0 && this.curPos < allinds.size()) {
			return new ConceptAssertion(allinds.get(curPos++),
					KBVocabulary.CONCEPT_TOP);
		}
		return null;
	}

	@Override
	public boolean isSortedById() {
		return sortInds;
	}

}
